
package ai;

import java.awt.image.BufferedImage;

public class ScreenImgBinWater
{
	public static BufferedImage getCleanWaterImg(BufferedImage waterImg, int minWidthPos, int maxWidthPos, int minHeightPos, int maxHeightPos)
	{
		int w = waterImg.getWidth();
		int h = waterImg.getHeight();
		BufferedImage cleanWaterImg = new BufferedImage(w, h, BufferedImage.TYPE_BYTE_BINARY);
		for(int i = 0; i < w; i++)
		{
			for(int j = 0; j < h; j++)
			{
				if((i >= minWidthPos) && (i <= maxWidthPos) && (j >= minHeightPos) && (j <= maxHeightPos))
				{
					cleanWaterImg.setRGB(j, i, waterImg.getRGB(j, i));
				}
			}
		}
		return cleanWaterImg;
	}
	
	public static float [][] getWaterPercent(BufferedImage cleanedWaterImg, int [] equalWidthPos, int [] equalHeightPos)
	{
		float [][] imgInfo = new float[6][6];
		for(int i = 0; i < imgInfo.length; i++)
		{
			for(int j = 0; j < imgInfo[i].length; j++)
			{
				int whitePoint = 0;
				float total = (equalWidthPos[i + 1] - equalWidthPos[i]) * (equalHeightPos[j + 1] - equalHeightPos[j]);
//				System.out.println("第" + i + "," + j + "格,起始位置为：" + equalHeightPos[j] + "," + equalWidthPos[i] + "\t" + equalHeightPos[j + 1] + "," + equalWidthPos[i + 1]);
				for(int k = equalWidthPos[i]; k < equalWidthPos[i + 1]; k++)
				{
					for(int l = equalHeightPos[j]; l < equalHeightPos[j + 1]; l++)
					{
						if(cleanedWaterImg.getRGB(l, k) == -1)
						{
							whitePoint++;
						}
					}
				}
				float percent = whitePoint / total;
				imgInfo[j][i] = percent;
//				System.out.println(whitePoint + "/" + total + "=" + percent);
			}
		}
		return imgInfo;
	}
	
	public static float [][] getWaterPercent(BufferedImage cleanedWaterImg, int minWidthPos, int maxWidthPos, int minHeightPos, int maxHeightPos)
	{
		int h = maxHeightPos - minHeightPos;//获取识别区的高  
		int w = maxWidthPos - minWidthPos;//获取识别区的宽  
		System.out.println(minWidthPos + "minW");
		System.out.println(minHeightPos + "minH");
//		System.out.println("图像宽高为：" + w + "," + h);
		int perh = h / 6;
		int perw = w / 6;
		float [][] imgInfo = new float[6][6];
		float total = perh * perw;
		for(int i = 0; i < imgInfo.length; i++)
		{
			for(int j = 0; j < imgInfo[i].length; j++)
			{
				int whitePoint = 0;
				for(int k = minHeightPos; k < perh; k++)
				{
					for(int l = minWidthPos; l < perw; l++)
					{
						int xp = (i * perh) + k;
						int yp = (j * perw) + l;
						if(cleanedWaterImg.getRGB(yp, xp) == -1)
						{
							whitePoint++;
						}
					}
				}
				float percent = whitePoint / total;
				imgInfo[i][j] = percent;
			}
		}
		return imgInfo;
	}
	
	public static int [][] getWaterState(float [][] imgInfo)
	{
		int [][] result = new int[imgInfo.length][6];
		for(int i = 0; i < imgInfo.length; i++)
		{
			for(int j = 0; j < imgInfo[i].length; j++)
			{
				float imgIf = imgInfo[j][i];
				if((imgIf >= 0) && (imgIf < 0.04))
				{
					result[i][j] = 0;
				}else if((imgIf >= 0.04) && (imgIf < 0.20))
				{
					result[i][j] = 1;
				}else if((imgIf >= 0.20) && (imgIf < 0.40))
				{
					result[i][j] = 2;
				}else if((imgIf >= 0.4) && (imgIf < 0.60))
				{
					result[i][j] = 3;
				}else if((imgIf >= 0.60) && (imgIf < 0.9))
				{
					result[i][j] = 4;
				}
			}
		}
		return result;
	}
}
